package com.example.gybicycle.controller.web;
import  com.alibaba.fastjson.JSON;

import com.example.gybicycle.entity.Bike;
import com.example.gybicycle.service.BikeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.example.gybicycle.common.Log.SysLoging;
import com.example.gybicycle.common.entity.NormalPage;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.util.*;
import com.example.gybicycle.entity.BikeFailure;
import com.example.gybicycle.entity.LoginInfo;
import com.example.gybicycle.entity.SysUser;
import com.example.gybicycle.service.BikeFailureService;



@Tag(name = "故障")
@RestController
@RequestMapping("/web/bikeFailure")
public class BikeFailureController {
    @Autowired
    private BikeFailureService failureService;
    @Autowired
    private BikeService bikeService;
    @PostMapping("/get")
    @SysLoging("故障管理-查看")
    @Operation(summary = "根据id查看")
    @RequiresPermissions("failure:get")
    @ApiOperationSupport(includeParameters = {"failure.id"})
    public Result get(@RequestBody BikeFailure failure){
      failure=failureService.get(failure.getId());
        return Result.success(failure);
    }
    @PostMapping("/list")
    @Operation(summary =  "列表查看")
    @ApiOperationSupport(includeParameters = {"page.id","page.page","page.size","page.name","failure.state"})
    @RequiresPermissions("failure:list")
    @SysLoging("故障管理-查看")
    public Result findList(@RequestBody(required=false)  BikeFailure page){
        Page<BikeFailure> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->failureService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/confirm")
    @Operation(summary =  "故障确认")
    @ApiOperationSupport(includeParameters = {"page.id","page.page","page.size","page.name","failure.state"})
    @RequiresPermissions("failure:confirm")
    public Result confirm(@RequestBody(required=false)  BikeFailure page,HttpServletRequest request){
        LoginInfo info=LoginUtil.getLoginInfo(request);
        if(info==null)
            return Result.loginOut();
        if(page.getId()==null||page.getFailureState()==null||page.getFailureState().equals(0))
            return Result.fail("故障ID或故障状态获取错误");
        BikeFailure failure=new BikeFailure();
        failure.setId(page.getId());
        failure.setFailureState(page.getFailureState());
        failure.setConfirmBy(info.getUser().getId());
        failure.setConfirmTime(new Date());
        int num=failureService.update(failure);
        if(num>0){
            if(page.getFailureState().equals(1)){
                Bike bike=new Bike();
                bike.setBikeState(2);
                if(page.getBikeId()==null)
                    page=failureService.get(page.getId());
                bike.setId(page.getBikeId());
                bikeService.update(bike);
            }
            return Result.success( page);
        }
        else return Result.fail("故障确认失败");
    }
   /*  @PostMapping("/save")
    @Operation(summary =  "保存")
    @RequiresPermissions("failure:save")
    @SysLoging("故障管理-保存")
    public Result save(@RequestBody BikeFailure failure, HttpServletRequest request){
         LoginInfo loginInfo=LoginUtil.getLoginInfo(request);
        if(loginInfo==null)
            return Result.loginOut();
        return failureService.save(failure,1);
    }*/
 /* @PostMapping("/delete")
    @Operation(summary =  "删除")
    @RequiresPermissions("failure:delete")
    @SysLoging("故障管理-删除")
    public Result delete(@RequestBody BikeFailure entity){
        int num=failureService.delete(entity.getId());
        if(num>0)
            return Result.success("删除成功");
        else 
            return Result.fail("删除失败");
    }*/

}